package io.renren.modules.ProjectControl.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.common.utils.R;
import io.renren.modules.ProjectControl.Service.taskService.TaskServiceP3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.*;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Service
@RefreshScope
public class TaskServiceP3iml implements TaskServiceP3 {
    @Value("${auth.centerLocation}")
    private  String AuthCenter;
    @Value("${project.location}")
    private String projectLoaction;
    @Autowired
    private RestTemplate restTemplate;
    @Override
    //模型列表
    public R getList(Map<String, Object> params,String token) {
        System.out.println(AuthCenter);
        System.out.println(projectLoaction);
        System.out.println(token);
        R r=R.ok();
        Map<String, Object> res=new HashMap<String,Object>();
       R rr= restTemplate.getForObject(projectLoaction+"/model/datamodel/list?token="+token,R.class,params);
        System.out.println(rr);
         return  rr;
    }

    @Override
    //模型属性列表
    public R getPropList(Map<String, Object> params, String token) {
        R rr= restTemplate.getForObject(projectLoaction+"/model/dicmodelprop/list?token="+token,R.class,params);
        System.out.println(rr);
        return  rr;
    }

    @Override
    public R getDataapsTaskList(Map<String, Object> params, String token) {
        R rr= restTemplate.getForObject(projectLoaction+"/model/dataapstask/list?token="+token,R.class,params);
        return rr;
    }

    @Override
    public String getDataApsTaskAllMoudles(String token) {
        return   restTemplate.getForObject(projectLoaction+"/model/dataapstask/findAllModules?token="+token,String.class);

    }

    @Override
    public R getAllDataInfo(String token) {
        R rr= restTemplate.getForObject(projectLoaction+"/model/datamodel/allDataInfo?token="+token,R.class);
        return rr;
    }

    @Override
    public String getModuleById(String id,String token) {
        return restTemplate.getForObject(projectLoaction+"/model/dataapstask/getModule/"+id+"?token="+token,String.class);
    }

    @Override
    public Boolean saveMoule(String params, String token) {
//        HttpHeaders httpHeaders = new HttpHeaders();
        MultiValueMap<String,Object> param = new LinkedMultiValueMap<String, Object>();

        param.add("Aps",params);


        return restTemplate.postForObject(projectLoaction+"/model/dataapstask/saveModule?token="+token, param,Boolean.class);

    }

    @Override
    public R getOriginalSchedule(MultipartFile file, String Id, String token) throws IOException {

        MultiValueMap<String,Object> parts = new LinkedMultiValueMap<>();

//        InputStreamSource inputStreamSource=new InputStreamSource();
        ByteArrayResource fileAsResource = new ByteArrayResource(file.getBytes()) {
            @Override
            public String getFilename() {
                return file.getOriginalFilename();
            }
            @Override
            public long contentLength() {
                return file.getSize();
            }
        };
        parts.add("file", fileAsResource);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> requestEntity
                = new HttpEntity<>(parts, headers);
        System.out.println(token);
        return  restTemplate.postForObject(projectLoaction+"/originalSchedule/getOriginalSchedule/"+Id+"?token="+token,requestEntity,R.class);

    }

    @Override
    public R deleteApsTask(Integer[] ids, String token) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        String ids_= objectMapper.writeValueAsString(ids);
        System.out.println(ids_);
        MultiValueMap<String,Object> param=new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        param.add("data", ids_);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("token",token);

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param,headers);




        return restTemplate.postForObject(projectLoaction+"/model/dataapstask/delete?token="+token, ids,R.class);

    }


}
